'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE } = app.Sequelize;

  const Question = app.model.define('question', {
    question_id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '题目编号',
    },
    category_id: {
      type: INTEGER,
      allowNull: false,
      comment: '分类编号',
    },
    question_content: {
      type: TEXT,
      allowNull: false,
      comment: '题目内容',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      comment: '创建时间',
    },
  }, {
    tableName: 'question',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: false,
    comment: '题目表',
  });

  // 关联关系
  Question.associate = function() {
    // 一个题目属于一个分类
    app.model.Question.belongsTo(app.model.QuestionCategory, {
      foreignKey: 'category_id',
      as: 'category',
    });
    
    // 一个题目可以有多个答案
    app.model.Question.hasMany(app.model.QuestionAnswer, {
      foreignKey: 'question_id',
      as: 'answers',
    });
  };

  return Question;
};
